package com.yhy;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;


public class HelloServerHandler extends SimpleChannelInboundHandler<String> {
    //有数据可读的时候触发
    @Override
    protected void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception {
        //处理消息
        System.out.println(getClass().getSimpleName() + "." + "channelRead0" + ctx.channel().remoteAddress() + ":" + msg);
        //返回消息
        ctx.channel().writeAndFlush("recvied message");
    }

    //客户端链接的时候触发
    @Override
    public void channelRegistered(ChannelHandlerContext ctx) throws Exception {
        System.out.println(getClass().getSimpleName() + "." + "channelRegistered:"+ctx.channel().remoteAddress());
    }

    //客户端断开链接的时候触发
    @Override
    public void channelUnregistered(ChannelHandlerContext ctx) throws Exception {
        System.out.println(getClass().getSimpleName() + "." + "channelUnregistered:"+ctx.channel().remoteAddress());
    }

    //客户端链接上来的时候触发
    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        System.out.println(getClass().getSimpleName() + "." + "channelActive:"+ctx.channel().remoteAddress());
    }

    //客户端断开链接的时候触发
    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
        System.out.println(getClass().getSimpleName() + "." + "channelInactive:"+ctx.channel().remoteAddress());
    }

    //完成一次读操作的时候触发(1.read 0 断开的时候 2.正常读完一次数据)
    @Override
    public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
        System.out.println(getClass().getSimpleName() + "." + "channelReadComplete:"+ctx.channel().remoteAddress());
    }

    //用户自定义的触发，后面超时无数据会用到
    @Override
    public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
        System.out.println(getClass().getSimpleName() + "." + "userEventTriggered:"+ctx.channel().remoteAddress());
    }

    //Channel的可写状态变化通知事件
    @Override
    public void channelWritabilityChanged(ChannelHandlerContext ctx) throws Exception {
        System.out.println(getClass().getSimpleName() + "." + "channelWritabilityChanged:"+ctx.channel().remoteAddress());
    }

    //异常的时候触发
    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        System.out.println(getClass().getSimpleName() + "." + "exceptionCaught:"+ctx.channel().remoteAddress()+
        " "+cause.getMessage());
    }
}
